package com.mcxx.modules.temporary.order.service.read;

import com.mcxx.modules.filemanage.file.service.FileService;
import com.mcxx.modules.miniSalvation.check.dto.CheckFileDTO;
import com.mcxx.modules.miniSalvation.order.dto.CheckDTO;
import com.mcxx.modules.miniSalvation.order.param.ReferenceParam;
import com.mcxx.modules.temporary.order.entity.TmpOrderCertEntity;
import com.mcxx.modules.temporary.order.dao.read.TmpOrderCertReadDao;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import com.yinhai.yhdcs.core.common.DcsServerException;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yinhai.ta404.core.service.BaseService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.*;
import java.util.stream.Collectors;

@Service("tmpOrderCertReadService")
@TaTransactional
public class TmpOrderCertReadService extends BaseService{

    private final static String BASE_PATH = "/reference/v2/upload_images/";
    @Autowired
    private TmpOrderCertReadDao tmpOrderCertReadDao;
    @Autowired
    private FileService fileService;


    public List<TmpOrderCertEntity> listAll() {
    	return tmpOrderCertReadDao.listAll();
    }



    public int cntInhouseCertNum(String orderId, String orderInhouseId){
        return tmpOrderCertReadDao.cntInhouseCertNum(orderId, orderInhouseId);
    }



    public TmpOrderCertEntity getById(String id) {
    	return tmpOrderCertReadDao.getById(id);
    }


    public  List<String> sendCheckVerifyFile(List<CheckDTO> checkFileList){

        String fileType = "'102','502'";

        String associationTable = "502,102";

        List<String> orderIdList = checkFileList.stream().map(CheckDTO::getOrderId).collect(Collectors.toList());

        List<String> noExitCertOrderIdList = this.getNoExistCertOrderId(fileType, associationTable, orderIdList);

        List<String> notExitCertIdcardList = checkFileList.stream()
                .filter(checkDTO -> noExitCertOrderIdList.contains(checkDTO.getOrderId()) == true)
                .map(CheckDTO::getIdcard)
                .collect(Collectors.toList());
        return notExitCertIdcardList;
    }


    public List<String> getNoExistCertOrderId(String fileType, String associationTable, List<String> orderIdList){
        List<String> notExistCertOrderList = tmpOrderCertReadDao.selectByOrderId(orderIdList);
        //List<String> existCertOrderList = orderIdList.stream().filter(orderId->notExistCertOrderList.contains(orderId) == false).collect(Collectors.toList());
        return  notExistCertOrderList;
    }


    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public List<Map<String, String>> listByType(ReferenceParam referenceParam) {
        List<Map<String, String>> list = tmpOrderCertReadDao.listByType(referenceParam);
        //list.forEach(map -> {map.put("url", BASE_PATH + map.get("url")); });
        list.forEach(map -> {
            try {
                String imgUrl = fileService.getFileUrl(map.get("url"),map.get("bucketId"));
                map.put("url", imgUrl);
            } catch (DcsServerException e) {
                map.put("url", "#");
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus()
                        .setRollbackOnly();
            }
        });

        return list;
    }


    public List<Map<String, Object>> countByTypes(ReferenceParam countParam) {
        List<Map<String, Object>> countResult = tmpOrderCertReadDao.countByTypes(countParam);
        if (countResult.size() == countParam.getTypes().length) {
            return countResult;
        }

        Set<String> types = new HashSet<>(Arrays.asList(countParam.getTypes()));
        if (countResult.size() == 0) {
            fillCountResult(types,countResult);
            return countResult;
        }


        Set<String> actTypes = new HashSet<>();
        countResult.forEach(countMap -> {
            actTypes.add((String) countMap.get("type"));
        });


        types.removeAll(actTypes);

        fillCountResult(types,countResult);

        return countResult;
    }


    private void fillCountResult(Set<String> types,List<Map<String,Object>> result) {
        types.forEach(type -> {
            HashMap<String, Object> map = new HashMap<>();
            map.put("type",type);
            map.put("count",0);
            result.add(map);
        });
    }


    public String getFileKeyById(String id) {
        return tmpOrderCertReadDao.getFileKeyById(id);
    }


    public List<String> listFileKeyByIds(String[] ids) {
        return tmpOrderCertReadDao.listFileKeyByIds(ids);
    }


    public int queryCertIsExiten(@Param("orderIdList") List<String> orderIdList, String fileType, String associationTable){
        return tmpOrderCertReadDao.queryCertIsExiten(orderIdList,fileType,associationTable);
    }


    public Integer getUserLevelById(String id) {
        return tmpOrderCertReadDao.getUserLevelById(id);
    }


    public TmpOrderCertEntity getFileKeyAndUserLevelById(String id) {
        return tmpOrderCertReadDao.getFileKeyAndUserLevelById(id);
    }


    public List<TmpOrderCertEntity> cntNoticeCertNum(List<String> batchNoList){
        return tmpOrderCertReadDao.cntNoticesCertNum(batchNoList);
    }


    public List<CheckFileDTO> getFileByMemberId(String memberId,String orderId){
        return  tmpOrderCertReadDao.getFileByMemberId(memberId,orderId);
    }

   public  List<CheckFileDTO> getAuthorFile(String fileType,String orderId){
        return tmpOrderCertReadDao.getAuthorFile(fileType,orderId);
    }


    public int cntNoticeCertNum(String batchId){
        return tmpOrderCertReadDao.cntNoticeCertNum(batchId);
    }

    public Map<String,Long> selectTmpMustCert(String orderId,String familyId){
        return tmpOrderCertReadDao.selectTmpMustCert(orderId,familyId);
    }

    public List<String> selectTmpMemberNoCert(String orderId,String familyId){
        return tmpOrderCertReadDao.selectTmpMemberNoCert(orderId,familyId);
    }

    public List<String> selectFamilyTmpMustCert(List<String> familyIds){
        return tmpOrderCertReadDao.selectFamilyTmpMustCert(familyIds);
    }


    public List<String> selectFamilyTmpMustCerts(List<String> familyIds,List<String> orderIds){
        return tmpOrderCertReadDao.selectFamilyTmpMustCerts(familyIds,orderIds);
    }

    public List<String> selectNoIdcardCert(List<String> familyIds){
        return tmpOrderCertReadDao.selectNoIdcardCert(familyIds);
    }

    public List<String> selectNoIdcardCerts(List<String> familyIds,List<String> orderIds){
        return tmpOrderCertReadDao.selectNoIdcardCerts(familyIds,orderIds);
    }


    public 	List<TmpOrderCertEntity> selectBySelective(TmpOrderCertEntity tmpOrderCertEntity){
        return   tmpOrderCertReadDao.selectBySelective(tmpOrderCertEntity);
    }

    public List<String> getIntegrateAuthorFile(String fileType,String orderId){
        return tmpOrderCertReadDao.getIntegrateAuthorFile(fileType,orderId);
    }
}
